package com.bot.adms.utils;

import java.sql.*;

/*访问数据库的工具类*/
public class JdbcUtils {
    //可以把几个字符串定义成常量：用户名，密码，URL，驱动类
    private static final String USER = "root";
    private static final String PWD = "root";
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/bo_adms?characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";

    /*注册驱动*/
    static {
        try {
            System.out.println("开始加载MySQL驱动...");
            System.out.println("驱动类名: " + DRIVER);
            Class<?> driverClass = Class.forName(DRIVER);
            System.out.println("成功加载MySQL驱动类: " + driverClass.getName());
            // 尝试创建驱动实例
            Driver driver = (Driver) driverClass.getDeclaredConstructor().newInstance();
            System.out.println("成功创建MySQL驱动实例: " + driver);
            // 注册驱动
            DriverManager.registerDriver(driver);
            System.out.println("MySQL驱动注册成功");
        } catch (ClassNotFoundException e) {
            System.err.println("无法加载MySQL驱动类: " + DRIVER);
            System.err.println("错误原因: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("数据库驱动加载失败", e);
        } catch (Exception e) {
            System.err.println("初始化MySQL驱动时发生异常: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("数据库驱动初始化失败", e);
        }
    }

    /*得到数据库的连接*/
    public static Connection getConnection() throws SQLException {
        System.out.println("尝试连接数据库: " + URL);
        System.out.println("用户名: " + USER);
        Connection conn = DriverManager.getConnection(URL,USER,PWD);
        System.out.println("数据库连接成功");
        return conn;
    }

    public static void closeConnection(Connection conn){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void closeStatement(Statement stmt){
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void closePreparedStatement(PreparedStatement pstmt){
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {}
        }
    }
    public static void closeResultSett(ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*关闭所有打开的资源*/
    public static void close(Connection conn, Statement stmt) {
        closeConnection(conn);
        closeStatement(stmt);
    }

    /**
     * 关闭连接和PreparedStatement资源
     */
    public static void close(Connection conn, PreparedStatement pstmt) {
        closeConnection(conn);
        closePreparedStatement(pstmt);
    }
    /*关闭所有打开的资源*/
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        closeConnection(conn);
        closeStatement(stmt);
        closeResultSett(rs);
    }

    public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        closeConnection(conn);
        closePreparedStatement(pstmt);
        closeResultSett(rs);
    }
}
